package com.yitao.cms.service.expressage;

import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.json.JSON;
import com.skipper.base.json.JSONObject;
import com.yitao.client.model.order.ExpressageSubscribeLogModel;
import com.yitao.cms.constant.UserSession;
import com.yitao.cms.utils.BaseUtils;
import com.yitao.cms.utils.Kuaidi100CmsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;


/**
 * 快递100单号订阅
 *
 * @Date: 2019/6/11 20:02
 * @Author: Kien
 */
@Service
public class Kuaidi100CmsService {
    private BaseUtils base = BaseUtils.getInstance();
    private static Logger logger = LoggerFactory.getLogger(Kuaidi100CmsService.class);
    @Autowired
    private ExpressageSubscribeLogCmsService expressageSubscribeLogCmsService;
    @Value("${subscribe_callback_url}")
    private String SUBSCRIBECALLBACKURL;

    /**
     *
     * 功能描述: 快递100物流信息订阅
     *
     * @param
     * @return
     * @auther: Kien
     * @date: 2019/5/15 18:00
     */
    @Transactional
    public DataOutput subscribeData(String company, String number, String phone){
        if(base.parametersIsBlank(company, number)) {
            logger.info("快递单号和快递公司编码不能为空");
            return new DataOutput(CommonStatus.BAD_REQUEST.getCode(), "快递单号和快递公司编码不能为空");
        }
//        String phone = null;
        String s = Kuaidi100CmsUtils.subscriptionKuaidi100(company, number, SUBSCRIBECALLBACKURL, phone);
        JSONObject jsonObject1 = JSON.parseObject(s);
        expressageSubscribeLogCmsService.save(company, number, jsonObject1.getString("returnCode"), jsonObject1.getString("result"));
        String returnCode = jsonObject1.getString("returnCode");
        String result = jsonObject1.getString("result");
        String message = jsonObject1.getString("message");
        logger.info("returnCode: "+returnCode + "result: " + result);
        return new DataOutput(CommonStatus.SUCCESS.getCode(), "再次订阅结果: " + message);
    }

    /**
     *
     * 功能描述:
     *
     * @param id 物流订阅日志id
     * @param request
     * @return
     * @auther: Kien
     * @date: 2019/6/25 17:42
     */
    public DataOutput againSubscribe(Long id, HttpServletRequest request) {
        Long userId = UserSession.getUserIdBySession(request);
        if(null == userId) {
            return new DataOutput(CommonStatus.UNAUTHORIZED);
        }
        if(null == id) {
            return new DataOutput(CommonStatus.NOT_FOUND.getCode(), "参数为空，检查后重试");
        }
        ExpressageSubscribeLogModel expressageSubscribe = expressageSubscribeLogCmsService.getExpressageSubscribeById(id);
        if(expressageSubscribe == null) {
            return new DataOutput(CommonStatus.NOT_FOUND.getCode(), "未找到指定物流信息");
        }
        return subscribeData(expressageSubscribe.getDeliveryCompanyCode(), expressageSubscribe.getDeliveryNo(), null);
    }
}
